package com.yx.cloud.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.yx.cloud.R;
import com.yx.cloud.product.sdk.tmall.MusicInfo;
import com.yx.cloud.utils.RomUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SubCateViewAdapter extends RecyclerView.Adapter implements View.OnClickListener{
    private static final String TAG = "SubCateViewAdapter";
    private Context context;
    private List<MusicInfo> mMusicInfoList;
    private InnerItemOnclickListener listener;
    private int currentPos = -1;
    private Set<String> iconUriSet;
    private RequestOptions options;
    private long cateId;
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.sub_cate_list_item,parent,false);
        SubCateViewAdapter.MyViewHolder viewHolder = new SubCateViewAdapter.MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MusicInfo musicInfo = mMusicInfoList.get(position);
        SubCateViewAdapter.MyViewHolder myViewHolder = (SubCateViewAdapter.MyViewHolder) holder;
        myViewHolder.rlAlbumCover.setOnClickListener(this);
        myViewHolder.rlAlbumCover.setTag(position);

        String icon = musicInfo.getCoverUrl();
        if(icon == null || icon.equals("")){
            if(musicInfo.getCoverDrawableId() != -1){
                Glide.with(context).load(musicInfo.getCoverDrawableId()).placeholder(R.mipmap.music_cover_default).apply(options).into(myViewHolder.ivMusicIcon);
            }else {
                myViewHolder.ivMusicIcon.setImageResource(R.mipmap.music_cover_default);
            }
        }else {
            Glide.with(context).load(icon).placeholder(R.mipmap.music_cover_default).apply(options).into(myViewHolder.ivMusicIcon);
        }
        myViewHolder.tvMusicName.setText(musicInfo.getTitle());
        Log.d("music","play position + " + position);
    }

    public SubCateViewAdapter(Context context, List<MusicInfo> musicInfoList,long cateId) {
        this.context = context;
        this.iconUriSet = new HashSet<>();
        this.mMusicInfoList = musicInfoList;
        this.cateId = cateId;
        this.options = new RequestOptions()
                .bitmapTransform(new RoundedCorners(30));
    }

    public void  updateData(List<MusicInfo> musicInfoList){
        this.mMusicInfoList = musicInfoList;
        notifyDataSetChanged();
    }

    public void  selectItem(int position){
        currentPos = position;
        notifyDataSetChanged();
    }

    public MusicInfo getItem(int position){
        return mMusicInfoList.get(position);
    }

    public int getSelectedItem(){
        return currentPos;
    }

    @Override
    public int getItemCount() {
        return mMusicInfoList.size();
    }


    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        currentPos = (int)v.getTag();
        listener.itemClick(v,(int)v.getTag());
    }

    public interface InnerItemOnclickListener {
        void itemClick(View v,int position);
    }

    public void setOnInnerItemOnClickListener(SubCateViewAdapter.InnerItemOnclickListener listener) {
        this.listener = listener;
    }

    class MyViewHolder extends  RecyclerView.ViewHolder{
        RelativeLayout rlAlbumCover;
        ImageView ivMusicIcon;
        TextView tvMusicName;
        public MyViewHolder(View itemView) {
            super(itemView);
            rlAlbumCover = itemView.findViewById(R.id.rl_sub_cate_cover);
            ivMusicIcon = itemView.findViewById(R.id.iv_sub_cate_item_icon);
            tvMusicName = itemView.findViewById(R.id.tv_sub_cate_name);
        }
    }
}
